import {
  queryPointList, actionPointDel, actionPointExport, actionPointImport, saveAddPoint,saveEditPoint,
  queryPointInfo,actionPointChange,queryDeviceInfoList
} from '../services/point';


export default {
  namespace: 'point',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    log:{
      deviceList:{}
    },
    detaildata:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_point_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryPointList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },


    // 批量删除操作
    *action_point_del({ payload }, { call, put }) {
      const response = yield call(actionPointDel, payload);
    },
    // 文件导出
    *action_point_export({ payload }, { call, put }) {
      const response = yield call(actionPointExport, payload);
    },
    // 文件导入
    *action_point_import({ payload }, { call, put }) {
      const response = yield call(actionPointImport, payload);
    },
    // 点位 关联区域 更换
    *action_point_change({ payload }, { call, put }) {
      const response = yield call(actionPointChange, payload);
    },


    // 新增
    *add_point({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveAddPoint,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 编辑
    *edit_point({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveEditPoint,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 获取单个详情
    *fetch_point_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryPointInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },

    //获取点位下的设备信息
    *fetch_device_info({ payload }, { call, put }) {
      const response = yield call(queryDeviceInfoList, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{deviceList:response.data},
        })
      );

    },



  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    // appendSelectSource(state, action) {
    //   return {
    //     ...state,
    //     selectSource: {
    //       ...state.selectSource,
    //       ...action.payload,
    //     },
    //   };
    // },


    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendDataLog(state, action) {
      return {
        ...state,
        log: {
          ...state.log,
          ...action.payload,
        },
      };
    },


    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        loading:{}
      };
    },

  },
};
